/*
 * $Id: InternalFrameListenerDelegate.java 23 2009-03-31 23:37:36Z aaron.day $
 */
package org.crazydays.gameplan.swing;


import javax.swing.Action;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.crazydays.gameplan.GamePlan;

/**
 * InternalFrameListenerDelegate delegates internal frame events to the gameplan
 * object.
 * <p>
 * <b>NOTE:</b><br>
 * This listener delegate breaks the normal registration rules for internal
 * frame listeners and the actions defined for the GamePlan object. This is done
 * so all of the actions do not need to be registered.
 * </p>
 */
public class InternalFrameListenerDelegate
        implements InternalFrameListener
{
	/** log */
	protected Log LOG = LogFactory.getLog(getClass());

	/** game plan */
	protected GamePlan gamePlan;

	/**
	 * InternalFrameListenerDelegate constructor.
	 * 
	 * @param gamePlan GamePlan
	 */
	public InternalFrameListenerDelegate(GamePlan gamePlan)
	{
		setGamePlan(gamePlan);
	}

	/**
	 * Set game plan.
	 * 
	 * @param gamePlan GamePlan
	 */
	public void setGamePlan(GamePlan gamePlan)
	{
		this.gamePlan = gamePlan;
	}

	/**
	 * Get game plan.
	 * 
	 * @return GamePlan
	 */
	public GamePlan getGamePlan()
	{
		return gamePlan;
	}

	/**
	 * Notify all of the actions that the internal frame has been activated.
	 * 
	 * @param event Event
	 * @see InternalFrameListener#internalFrameActivated(InternalFrameEvent)
	 */
	public void internalFrameActivated(InternalFrameEvent event)
	{
		LOG.debug(event);

		// notify all actions that the internal frame has changed
		// iterate through the actions looking for frames which listen
		// for internal frame events and remove them to the internal
		// frame
		for (Action action : getGamePlan().getActions().values()) {
			if (action instanceof InternalFrameListener) {
				((InternalFrameListener) action)
				        .internalFrameActivated(event);
			}
		}
	}

	/**
	 * Cleanup existing data pertaining to the closed internal frame.
	 * 
	 * @param event Event
	 * @see InternalFrameListener#internalFrameClosed(InternalFrameEvent)
	 */
	public void internalFrameClosed(InternalFrameEvent event)
	{
		LOG.debug(event);

		// remove ourself from the internal frame as a listener
		event.getInternalFrame().removeInternalFrameListener(this);
	}

	/**
	 * @param event Event
	 * @see InternalFrameListener#internalFrameClosing(InternalFrameEvent)
	 */
	public void internalFrameClosing(InternalFrameEvent event)
	{
		LOG.debug(event);
	}

	/**
	 * @param event Event
	 * @see InternalFrameListener#internalFrameDeactivated(InternalFrameEvent)
	 */
	public void internalFrameDeactivated(InternalFrameEvent event)
	{
		LOG.debug(event);
	}

	/**
	 * @param event Event
	 * @see InternalFrameListener#internalFrameDeiconified(InternalFrameEvent)
	 */
	public void internalFrameDeiconified(InternalFrameEvent event)
	{
		LOG.debug(event);
	}

	/**
	 * @param event Event
	 * @see InternalFrameListener#internalFrameIconified(InternalFrameEvent)
	 */
	public void internalFrameIconified(InternalFrameEvent event)
	{
		LOG.debug(event);
	}

	/**
	 * @param event Event
	 * @see InternalFrameListener#internalFrameOpened(InternalFrameEvent)
	 */
	public void internalFrameOpened(InternalFrameEvent event)
	{
		LOG.debug(event);
	}
}
